Skip to content

Make LongPoll sessions resumable by default - #6885

Open
SteffenDE wants to merge 1 commit into
mainfrom
sd-longpoll-resumable
Open

SteffenDE wants to merge 1 commit into
mainfrom
sd-longpoll-resumable

Conversation

@SteffenDE

Copy link
Copy Markdown
Member

Long-poll request failures currently close the socket and force channels to rejoin, even when the server session is still alive.
This change retries interrupted requests so channels can stay joined and outgoing pushes can be buffered during recovery.

The server retains message batches until acknowledged and deduplicates retried pushes using sequence numbers. The client exposes socket.onSuspend and socket.onResume callbacks for recovery status; push timeouts still apply. If the session expires, or an older server is encountered during a rolling deploy, the client falls back to the existing reconnect behavior.

Long-poll request failures currently close the socket and force channels to rejoin,
even when the server session is still alive.
This change retries interrupted requests so channels can stay joined
and outgoing pushes can be buffered during recovery.

The server retains message batches until acknowledged and deduplicates
retried pushes using sequence numbers. The client exposes `socket.onSuspend`
and `socket.onResume` callbacks for recovery status; push timeouts still apply.
If the session expires, or an older server is encountered during a rolling deploy,
the client falls back to the existing reconnect behavior.
@SteffenDE
SteffenDE marked this pull request as ready for review October 28, 2026 18:24
@SteffenDE

Copy link
Copy Markdown
Member Author

Resumable Long Poll.html

This is a Claude artifact describing the approach quite well.

SteffenDE added a commit that referenced this pull request Nov 6, 2026
A listen that waited for `window_ms` sent `:expired` to the session and
returned 204 right away. The session could still handle a flush that the
listen had sent before. It then sent its buffered messages to a process
that no longer read them and cleared the buffer, so the next listen
received nothing. This happens when the session is busy for longer than
`window_ms` while a flush of the listen is queued.

The listen now waits for the session before it returns. After the
window, it sends `:expired` followed by `:subscribe`, which the session
already echoes. The session receives the messages of a listen in the
order they were sent, so the echo tells the listen that no flush is
left:

  * If the session answered a flush, the messages arrive before the
    echo and the listen returns them.
  * If the echo arrives first, the listen returns 204 as before. The
    session keeps what it buffers afterwards for the next listen.
  * If neither arrives within `pubsub_timeout_ms`, the listen responds
    with status 410. It cannot tell whether the session still answers
    the flush. phoenix.js then starts a new session and rejoins its
    channels, as it does when a poll cannot resume its session.

When the messages arrive first, the echo stays unread. With Cowboy, the
request process stops after the response. Bandit ignores unhandled
messages.

Only the listen changes. The session code is the same, so nodes with and
without this change can serve the same session during a rolling deploy.
A listen served by a node without the change keeps the old behavior.

A poll against a session that stopped or is blocked now returns 410
after `window_ms` plus `pubsub_timeout_ms`. Before, it returned 204
after `window_ms`, and the next poll returned 410.

Messages can still be lost when the response does not reach the client.
That needs acknowledgements from the client, see #6885.

The tests hold the session before it handles a flush. They cover a
session that continues after the listen window and one that does not
answer in time, for Bandit and Cowboy, with direct and PubSub delivery.

The problem was reported in #6899.
SteffenDE added a commit that referenced this pull request Nov 6, 2026
A listen that waited for `window_ms` sent `:expired` to the session and
returned 204 right away. The session could still handle a flush that the
listen had sent before. It then sent its buffered messages to a process
that no longer read them and cleared the buffer, so the next listen
received nothing. This happens when the session is busy for longer than
`window_ms` while a flush of the listen is queued.

The listen now waits for the session before it returns. After the
window, it sends `:expired` followed by `:subscribe`, which the session
already echoes. The session receives the messages of a listen in the
order they were sent, so the echo tells the listen that no flush is
left:

  * If the session answered a flush, the messages arrive before the
    echo and the listen returns them.
  * If the echo arrives first, the listen returns 204 as before. The
    session keeps what it buffers afterwards for the next listen.
  * If neither arrives within `pubsub_timeout_ms`, the listen responds
    with status 410. It cannot tell whether the session still answers
    the flush. phoenix.js then starts a new session and rejoins its
    channels, as it does when a poll cannot resume its session.

When the messages arrive first, the echo stays unread. With Cowboy, the
request process stops after the response. Bandit ignores unhandled
messages.

Only the listen changes. The session code is the same, so nodes with and
without this change can serve the same session during a rolling deploy.
A listen served by a node without the change keeps the old behavior.

A poll against a session that stopped or is blocked now returns 410
after `window_ms` plus `pubsub_timeout_ms`. Before, it returned 204
after `window_ms`, and the next poll returned 410.

Messages can still be lost when the response does not reach the client.
That needs acknowledgements from the client, see #6885.

The tests hold the session before it handles a flush. They cover a
session that continues after the listen window and one that does not
answer in time, for Bandit and Cowboy, with direct and PubSub delivery.

Closes #6899.
@josevalim

Copy link
Copy Markdown
Member

This looks goods to me and my suggestion is to merge this to both main and v1.8 (so we can ship this on v1.8 first). v1.9 can them assume the client will send seq support and, if not given, emit error logs on both client and server. This means we can clean up a few other things, such as the several TODOs here and also keep only a single timer timestamp.

@SteffenDE

Copy link
Copy Markdown
Member Author

We should adjust this to use the message based numbering though if we plan to also go ahead with #6887. Right now it numbers the batches.

@josevalim

Copy link
Copy Markdown
Member

Yes, let's bring that feature then!

@josevalim

Copy link
Copy Markdown
Member

Perhaps the best plan is:

  1. PR: Add resumable long polling support into v1.8 and main (this PR)
  2. PR: Remove fallbacks from main
  3. PR: Introduce transport session exclusively to long polling
  4. PR: Add resume to websockets
  5. Ship new v1.8

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants